package com.example.demo.task;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class VipTask {
    private final UserMapper userMapper;

    public VipTask(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    @Transactional
    public void cleanExpiredVips() {
        List<User> users = userMapper.findAllWithVip(); // 假设有此方法
        for (User user : users) {
            if (user.getIsVip() && user.getVipExpiry() != null && user.getVipExpiry().isBefore(LocalDateTime.now())) {
                userMapper.updateVipStatus(user.getId(), false, null);
                log.info("用户 {} 的 VIP 已过期", user.getId());
            }
        }
    }

}
